﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Project
{
    public partial class Default : System.Web.UI.Page
    {
        SqlConnection connection = new SqlConnection(@"workstation id=StudentAdvisor.mssql.somee.com;packet size=4096;user id=ravi_mori_SQLLogin_1;pwd=ji71yfcnea;data source=StudentAdvisor.mssql.somee.com;persist security info=False;initial catalog=StudentAdvisor");
        SqlCommand command_login = null;
        SqlCommand command_UserId_Check = null;
        SqlDataReader dataReader = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userID"] == null)
                lbl_user.Text = "  Welcome Guest  ";
            else
                lbl_user.Text = " "+Session["userID"].ToString()+" ";
        }

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            txt_userID.Text = "";
            txt_password.Text = "";
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string UserIDCheck_string = "select * from tblUserLogin where UserId='" + txt_userID.Text + "'";
            command_UserId_Check = new SqlCommand(UserIDCheck_string, connection);

            string login_string = "select * from tblUserLogin where UserId='" + txt_userID.Text + "' and Password='" + txt_password.Text + "'";
            command_login = new SqlCommand(login_string, connection);

            try
            {
                connection.Open();
                dataReader = command_UserId_Check.ExecuteReader();
                if (dataReader.HasRows == false)
                {
                    dataReader.Close();
                    connection.Close();
                    lbl_loginError.Text = "This User ID has not registered.";
                }
                else
                {
                    dataReader.Close();
                    dataReader = command_login.ExecuteReader();
                    if (dataReader.HasRows == false)
                    {
                        dataReader.Close();
                        connection.Close();
                        lbl_loginError.Text = "Invalid Password.";
                    }
                    else
                    {
                        dataReader.Read();
                        Session["userID"] = txt_userID.Text;
                        Session["userType"] = dataReader["UserType"].ToString();
                        //lbl_loginError.Text = Session["userID"].ToString() + ", type= " + Session["userType"].ToString();
                        if (dataReader["UserType"].ToString() == "S")
                            Response.Redirect("studentProfile_.aspx");
                        else
                            Response.Redirect("advisorProfile.aspx");
                    }
                }
            }
            catch (SqlException ex)
            {
                lbl_loginError.Text = ex.ToString();
            }
            finally
            {
                if (!dataReader.IsClosed)
                {
                    dataReader.Close();
                    connection.Close();
                }
            }
        }
    }
}